﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NetCard.Models;
using NetCard.Dao;

namespace NetCard.Controllers
{
    public class ProjectController : Controller
    {
        private ProjectDao projectDao = new ProjectDao();

        //
        // GET: /Project/

        public ActionResult ProjectIndex()
        {
            String userLogin = User.Identity.Name;
            List<Project> projectList = projectDao.GetProjects(userLogin);
            return View(projectList);
        }

        //
        // GET: /Project/Create

        public ActionResult ProjectCreate()
        {
            Project projectToCreate = new Project();
            projectToCreate.timeout = 1500;
            projectToCreate.testInterval = 5;
            projectToCreate.ttl = 128;
            projectToCreate.size = 32;
            return View(projectToCreate);
        } 

        //
        // POST: /Project/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ProjectCreate([Bind(Exclude = "id")] Project projectToCreate)
        {
            if (!ModelState.IsValid)
                return View();
            try
            {
                String userLogin = User.Identity.Name;
                projectToCreate.userLogin = userLogin;
                projectDao.InsertProject(projectToCreate);
                return RedirectToAction("ProjectIndex");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Project/Edit/5

        public ActionResult ProjectEdit(long id)
        {
            var projectToEdit = projectDao.GetProject(id);               
            return View(projectToEdit);
        }

        //
        // POST: /Project/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ProjectEdit(Project projectToEdit)
        {
            if (!ModelState.IsValid)
                return View();

            try
            {
                var originalProject = projectDao.GetProject(projectToEdit.id);
                projectDao.UpdateProject(originalProject, projectToEdit);
                return RedirectToAction("ProjectIndex");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Project/Delete/5

        public ActionResult ProjectDelete(long id)
        {
            var projectToDelete = projectDao.GetProject(id);
            projectDao.DeleteProject(projectToDelete); 
            return RedirectToAction("ProjectIndex");
        }

        public ActionResult ProjectLoad(long id)
        {
            var projectToLoad = projectDao.GetProject(id);             
            Session["project"] = (Project)projectToLoad;         
            return RedirectToAction("RouterIndex", "Router", projectToLoad);
        }

        public void UpdatePosition(Project project)
        {

            var projectToEdit = projectDao.GetProject(project.id);
            projectToEdit.x = project.x;
            projectToEdit.y = project.y;
            this.ProjectEdit(projectToEdit);
        }

        public void UpdateZoom(Project project)
        {
            var projectToEdit = projectDao.GetProject(project.id);
            projectToEdit.zoom = project.zoom;
            this.ProjectEdit(projectToEdit);
        }

    }
}
